package com.frkj.order.controller;

import com.frkj.order.cache.BusinessCache;
import com.frkj.order.model.Business;
import com.frkj.order.util.ShowMessage;
import com.jfinal.core.Controller;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/6/27.
 */
public class BusinessController extends Controller {

    public void index() {
        List<Business> businessList = Business.me.find("select * from business order by id");
        setAttr("businessList", businessList);

        render("index.html");
    }

    public void add() {
        render("add.html");
    }

    public void save() {
        Business business = getModel(Business.class);
        business.set("create_time", new Date());
        business.save();

        BusinessCache.loadData();

        setAttr("showMessage", ShowMessage.ADD_SUCCESS);
        index();
    }

    public void edit() {
        setAttr("business", Business.me.findById(getParaToInt("id")));

        render("edit.html");
    }

    public void update() {
        getModel(Business.class).update();

        BusinessCache.loadData();

        setAttr("showMessage", ShowMessage.UPDATE_SUCCESS);
        index();
    }

    public void delete() {
        Business.me.deleteById(getParaToInt("id"));

        BusinessCache.loadData();

        setAttr("showMessage", ShowMessage.DELETE_SUCCESS);
        index();
    }
}
